<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>找回密码</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router@3.0.0/dist/vue-router.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
            <el-input v-model="ruleForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button @click="getCode('ruleForm')">获取邮箱验证码</el-button>
        </el-form-item>
        <el-form-item label="邮箱验证码">
            <el-input v-model="emailCode"></el-input>
        </el-form-item>
        <el-form-item label="输入图片验证码">
            <el-input v-model="imageCode"></el-input>
        </el-form-item>
        <img :src="codeUrl" @click="refreshCode"><a href="#" id="change"
                                                    @click="refreshCode">看不清？</a>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>


<script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validatePass3 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入验证码'));
                } else if (value !== this.realCode) {
                    callback(new Error('验证码错误'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    pass: '',
                    checkPass: '',
                    email: ''
                },
                emailCode: '',
                realCode: '',
                imageCode: '',
                codeUrl: '',
                reCode: '',
                rules: {
                    username: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
                    ],
                    pass: [
                        {required: true, validator: validatePass, trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
                    ],
                    checkPass: [
                        {required: true, validator: validatePass2, trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                        {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
                    ]
                }
            };
        },
        mounted() {
            this.refreshCode();
        },
        methods: {
            refreshCode() {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/webdemo/method/code/imageCode",
                    responseType: 'blob'
                }).then(function (resp) {
                    _this.codeUrl = URL.createObjectURL(new Blob([resp.data]));
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.emailCode !== this.realCode || this.emailCode === '') {
                            alert('邮箱验证码错误');
                        } else {
                            var _this = this;
                            axios({
                                method: "post",
                                url: "http://localhost:8080/webdemo/method/user/recover",
                                data: {
                                    username: _this.ruleForm.username,
                                    password: _this.ruleForm.pass,
                                    code: _this.imageCode
                                }
                            }).then(function (resp) {
                                if (resp.data.msg === "success") {
                                    alert("修改成功");
                                    location.href = 'LogIn.html';
                                } else {
                                    alert("修改失败,请检测验证码是否正确");
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            getCode(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var _this = this;
                        axios({
                            method: "post",
                            url: "http://localhost:8080/webdemo/method/code/sendRecoverCode",
                            data: {
                                username: _this.ruleForm.username,
                                email: _this.ruleForm.email
                            }
                        }).then(function (resp) {
                            if (resp.data.msg === 'success') {
                                _this.realCode = resp.data.data[0];
                                alert("发送验证码成功");
                            } else {
                                alert("发送失败，请检查邮箱与用户名");
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    })
</script>
</body>
</html>